package com.example.animaltiontext_1;

import android.content.Intent;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private ImageView mImage;
private Button mbtnstrt;
private Button mbtnstop;
private Button mBtnNext;
private static final int DURATION=80;
private AnimationDrawable manimation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        mImage = findViewById(R.id.image);
        mbtnstrt = findViewById(R.id.button_start);
        mbtnstop = findViewById(R.id.button_stop);
        mBtnNext = findViewById(R.id.button_next);
        manimation = (AnimationDrawable) mImage.getDrawable();
//        mImage.setImageDrawable(inianim());
        mbtnstop.setOnClickListener(this);
        mbtnstrt.setOnClickListener(this);
        mBtnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button_start){
            startAnim();
        }else if (id == R.id.button_stop){
            stopAnim();
        }else if (id == R.id.button_next) {
            Intent intent = new Intent(MainActivity.this, AnimationActivity.class);
            startActivity(intent);
            //overridePendingTransition(R.anim.scale_in, R.anim.no_anim);
        }
    }

    private void startAnim(){
        if (manimation != null){
            manimation.start();
        }
    }
    private void stopAnim(){
        if (manimation != null){
            manimation.stop();
        }
    }
//进入界面之后直接开始播放
    @Override
    protected void onResume() {
        super.onResume();
        Log.e("TAG", "onResume: ============");
        startAnim();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopAnim();
        Log.e("TAG", "onPause: ============" );
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("TAG", "onStop: =========");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("TAG", "onDestroy: ===============");
    }

    private AnimationDrawable inianim(){
        manimation = new AnimationDrawable();
        manimation.addFrame(getDrawable(R.drawable.f1),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f2),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f3),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f4),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f5),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f6),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f7),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f8),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f9),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f10),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f11),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f12),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f13),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f14),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f15),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f16),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f17),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f18),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f19),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f20),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f21),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f22),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f23),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f24),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f25),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f26),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f27),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f28),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f29),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f30),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f31),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f32),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f33),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f34),DURATION);
        manimation.addFrame(getDrawable(R.drawable.f35),DURATION);
        return manimation;
    }
}